﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Audio;

namespace Asteroids
{
    class Bullet : GameObject
    {
        public Bullet(Game g, Vector2 position, float rotation, float timeMade)
            : base(g)
        {
            Position = position;
            Rotation = rotation;
            timeAlive = timeMade;
            collided();

            Vector2 unitVector =
                new Vector2(
                    (float)Math.Cos(Rotation),
                    (float)Math.Sin(Rotation)
                );

            Velocity = 450f * unitVector;         //Bullet has constant velocity
        }
           
        Texture2D sprite;
        SpriteBatch spriteBatch;
        public float timeAlive { get; set; }         //gameTime that bullet was created

        protected override void LoadContent()
        {
            sprite = Game.Content.Load<Texture2D>("Asteroid-BulletN");
            spriteBatch = new SpriteBatch(Game.GraphicsDevice);
            SoundEffect soundEffect;
            soundEffect = Game.Content.Load<SoundEffect>("bullet");
            soundEffect.Play();
        }

        public override void Draw(GameTime gameTime)
        {
            Vector2 offset =
             new Vector2(sprite.Width / 2,
                         sprite.Height / 2);

            float elapsedSeconds = (float)gameTime.ElapsedGameTime.TotalSeconds;

            wrapAround(800, 500, sprite.Width, sprite.Height);     //Wraps around if obj goes off-screen

            boundingRectangle = new Rectangle((int)Position.X - (int)offset.X,      //Updates obj's boundingRectangle
                                              (int)Position.Y - (int)offset.Y, 
                                              sprite.Width, 
                                              sprite.Height);

            spriteBatch.Begin();
            spriteBatch.Draw(sprite,
                             Position,
                             null,
                             Color.White,
                             0,
                             offset,
                             1.0f,
                             SpriteEffects.None,
                             0);
            spriteBatch.End();
        }

        public override void Update(GameTime gameTime)
        {
            float elapsedSeconds = (float)gameTime.ElapsedGameTime.TotalSeconds;
            Position += Velocity * elapsedSeconds;
        } 
    }
}